﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.Extensions.Configuration;

namespace WT.OpenIddict.EntityFrameWorkCore;

public class OpenIddictDbConetextDesignTime : IDesignTimeDbContextFactory<OpenIddictDbContext>
{
    public IConfiguration Configuration { get; set; }

    public OpenIddictDbContext CreateDbContext(string[] args)
    {
        var optionsBuilder = new DbContextOptionsBuilder();
        var connectStr = GetConnectStr("Default");
        optionsBuilder.UseSqlServer(connectStr);
        return new OpenIddictDbContext(optionsBuilder.Options);
    }

    private string GetConnectStr(string name)
    {
        var Config = new ConfigurationBuilder().AddJsonFile("appsettings.json");
        var cfg = Config.Build();
        return cfg.GetConnectionString(name);
    }
}